package com.google.jia.impl.bmp;

import com.google.jia.api.exceptions.FileFormatException;
import com.google.jia.utils.DataConvertor;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: Егор
 * Date: 30.06.12
 * Time: 22:35
 * To change this template use File | Settings | File Templates.
 */
public class BitmapFileHeaderClass
{
    private static final byte[] BITMAP_TYPE_CONSTANT = new byte[]{(byte)'B', (byte)'M'};

    private static final byte[] NULL_DOUBLE_BYTE_ARRAY = new byte[2];

    private byte[] bfType = new byte[2];

    private byte[] bfSize = new byte[4];

    private byte[] bfReserved1 = new byte[2];

    private byte[] bfReserved2 = new byte[2];

    private byte[] bfOffBits = new byte[4];

    public byte[] getBfType() {
        return bfType;
    }

    public void setBfType(byte[] bfType) {
        this.bfType = bfType;
    }

    public byte[] getBfSize() {
        return bfSize;
    }

    public void setBfSize(byte[] bfSize) {
        this.bfSize = bfSize;
    }

    public byte[] getBfReserved1() {
        return bfReserved1;
    }

    public void setBfReserved1(byte[] bfReserved1) {
        this.bfReserved1 = bfReserved1;
    }

    public byte[] getBfReserved2() {
        return bfReserved2;
    }

    public void setBfReserved2(byte[] bfReserved2) {
        this.bfReserved2 = bfReserved2;
    }

    public byte[] getBfOffBits() {
        return bfOffBits;
    }

    public void setBfOffBits(byte[] bfOffBits) {
        this.bfOffBits = bfOffBits;
    }

    public long getRealSize()
    {
        return DataConvertor.bytesToLong(bfSize);
    }

    public static BitmapFileHeaderClass buildBitmapFileHeaderClass(InputStream inputStream) throws IOException
    {
        BitmapFileHeaderClass bitmapFileHeaderClass = new BitmapFileHeaderClass();
        //read file type
        if (inputStream.read(bitmapFileHeaderClass.bfType, 0, 2) == -1) throw new IOException("Unexpected file type");

        //read file size
        if (inputStream.read(bitmapFileHeaderClass.bfSize, 0, 4) == -1) throw new IOException("Unexpected file size");

        //read reserved
        if (inputStream.read(bitmapFileHeaderClass.bfReserved1, 0, 2) == -1) throw new IOException("Unexpected reserved bytes");

        //read reserved
        if (inputStream.read(bitmapFileHeaderClass.bfReserved2, 0, 2) == -1) throw new IOException("Unexpected reserved bytes");

        //read offset of image
        if (inputStream.read(bitmapFileHeaderClass.bfOffBits, 0, 4) == -1) throw new IOException("Unexpected offset bit result");

        return bitmapFileHeaderClass;
    }

    public static boolean validateBitmapFileHeaderClass(BitmapFileHeaderClass bitmapFileHeaderClass) throws IOException
    {
        if (!Arrays.equals(BITMAP_TYPE_CONSTANT, bitmapFileHeaderClass.bfType))
            throw new FileFormatException("The first 2 bytes of file should be 'BM' but its " + (char)bitmapFileHeaderClass.getBfType()[0] + (char)bitmapFileHeaderClass.getBfType()[1] );

        if (!Arrays.equals(NULL_DOUBLE_BYTE_ARRAY ,bitmapFileHeaderClass.bfReserved1)
                || !Arrays.equals(NULL_DOUBLE_BYTE_ARRAY ,bitmapFileHeaderClass.bfReserved2))
            throw  new FileFormatException("Reserved bytes should be filled by zeros");

        return true;
    }


}
